package com.chint.largeData.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chint.largeData.R;
import com.chint.largeData.Common.Constant;
import com.chint.largeData.tools.SharePreferencesUtil;
import com.chint.largeData.views.GestureContentView;
import com.chint.largeData.views.GestureDrawline.GestureCallBack;

public class ShowLockFragment extends Fragment implements OnClickListener{
	private View mView;
	private Activity mActivity;
	/** 手机号码*/
	public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER";
	/** 意图 */
	public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
	private RelativeLayout mTopLayout;
	private TextView mTextTitle;
	private TextView mTextCancel;
	private ImageView mImgUserLogo;
	private TextView mTextPhoneNumber;
	private TextView mTextTip;
	private FrameLayout mGestureContainer;
	private GestureContentView mGestureContentView;
	private TextView mTextForget;
	private TextView mTextOther;
	private String mParamPhoneNumber;
	private long mExitTime = 0;
	private int mParamIntentCode;
	private TextView tv_reset_passwd;

	private String mLockCode;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = getActivity();
		mView = inflater.inflate(R.layout.frag_show_lock, null);
		initData();
		initViews();
		return mView;
	}
	private void initViews() {
		if(TextUtils.isEmpty(mLockCode)){
			
		}
		tv_reset_passwd = (TextView) mView.findViewById(R.id.tv_reset_passwd);
		tv_reset_passwd.setOnClickListener(this);
		mTopLayout = (RelativeLayout) mView.findViewById(R.id.top_layout);
		mTextTitle = (TextView) mView.findViewById(R.id.text_title);
		mTextCancel = (TextView) mView.findViewById(R.id.text_cancel);
		mImgUserLogo = (ImageView) mView.findViewById(R.id.user_logo);
		mTextPhoneNumber = (TextView) mView.findViewById(R.id.text_phone_number);
		mTextPhoneNumber.setText(SharePreferencesUtil.getInstance(getActivity()).getValue(SharePreferencesUtil.USERNAME, ""));
		mTextTip = (TextView) mView.findViewById(R.id.text_tip);
		mGestureContainer = (FrameLayout) mView.findViewById(R.id.gesture_container);
		mTextForget = (TextView) mView.findViewById(R.id.text_forget_gesture);
		mTextOther = (TextView) mView.findViewById(R.id.text_other_account);
		
		
		// 初始化一个显示各个点的viewGroup
		mGestureContentView = new GestureContentView(mActivity, true, mLockCode,
				new GestureCallBack() {

					@Override
					public void onGestureCodeInput(String inputCode) {

					}

					@Override
					public void checkedSuccess() {
						mGestureContentView.clearDrawlineState(0L);
						//Toast.makeText(mActivity, "密码正确", 1000).show();
						mActivity.setResult(1);
						mActivity.finish();
					}

					@Override
					public void checkedFail() {
						mGestureContentView.clearDrawlineState(1300L);
						mTextTip.setVisibility(View.VISIBLE);
						mTextTip.setText(Html
								.fromHtml("<font color='#c70c1e'>密码错误</font>"));
						// 左右移动动画
						Animation shakeAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.shake);
						mTextTip.startAnimation(shakeAnimation);
					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(mGestureContainer);
	}
	private void initData() {
		mLockCode = SharePreferencesUtil.getInstance(mActivity).getValue(Constant.SP_KEY.LOCK_CODE, "");
	}
	@Override
	public void onClick(View arg0) {
		if(R.id.tv_reset_passwd==arg0.getId()){
//			SharePreferencesUtil.getInstance(mActivity).clear();
			SharePreferencesUtil.getInstance(mActivity).remove(SharePreferencesUtil.USERNAME);
			SharePreferencesUtil.getInstance(mActivity).remove(SharePreferencesUtil.PASSWORD);
			SharePreferencesUtil.getInstance(mActivity).remove(Constant.SP_KEY.LOCK_CODE);
			mActivity.finish();
		}
	}
}
